package com.copsoftware.support;

import java.io.IOException;
import java.io.InputStream;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;

import org.apache.struts.util.LabelValueBean;

public class BeanPropertiesProccessor {
	
	public static Set getFileProperties(String filePropertiesName) throws IOException
	{
		try {
			ClassLoader loader = Thread.currentThread().getContextClassLoader();
			
			//Use context classloader to read states.properties
			InputStream iStream = loader.getResourceAsStream(filePropertiesName);
			Properties props = new Properties();
			//Load the stream into the properties object directly
			props.load(iStream);
			
			//Look up by key and load them into a ArrayList as NameValuePair collection
			Enumeration keyEnum = props.propertyNames();
			Set stateSet = new TreeSet(
//					new Comparator() 
//					{
//						public int compare(Object a, Object b)
//						{
//							LabelValueBean nvpA = (LabelValueBean) a;
//							LabelValueBean nvpB = (LabelValueBean) b;
//							
//							String valA = nvpA.getLabel();
//							String valB = nvpB.getLabel();
//							return valA.compareTo(valB);
//						}
//					}
			);
			
			LabelValueBean nvp = null;
			String keyName = null;
			String label = null;
			while (keyEnum.hasMoreElements())
			{
				keyName = (String) keyEnum.nextElement();
				label = props.getProperty(keyName);
				nvp = new LabelValueBean(label, keyName);
				stateSet.add(nvp);
			}
			return stateSet;			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw e;
		}
	}
}
